package com.docc.docc;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.docc.model.DateDay;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class CalendarActivity extends AppCompatActivity {
    private TextView btnPrev;
    private TextView btnNext;
    private TextView txtDate;
    private GridView grid;
    private ViewPager viewPager;
    final private Calendar curDate = Calendar.getInstance() ;
    private DateDay dateDay;
    private ArrayList<DateDay> cells = new ArrayList<DateDay>();
    private Date now;
    private Date showDate;
    private int currPager = 500;
    private MyPagerAdapter pagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        bindControl();
        bindControlEven();
        bindPagerView();
//        renderCalendar();
    }
    public void bindControl(){
        btnPrev = (TextView)findViewById(R.id.btnPrev);
        btnNext = (TextView)findViewById(R.id.btnNext);
        txtDate = (TextView)findViewById(R.id.txtDate);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    public void bindControlEven(){
        btnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                curDate.add(Calendar.MONTH, -1);
//                renderCalendar();
                viewPager.setCurrentItem(--currPager, true);
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                curDate.add(Calendar.MONTH, 1);
//                renderCalendar();
                viewPager.setCurrentItem(++currPager, true);
            }
        });
    }

    public void bindPagerView(){
        initTitle(curDate);
        pagerAdapter = new MyPagerAdapter();
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(500);// 设置当前页位置为500
        viewPager.setPageMargin(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Calendar tempSelected = Calendar.getInstance(); // 临时
                int count = position - 500;
                tempSelected.add(Calendar.MONTH, count);
                initTitle(tempSelected);
                currPager = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void renderCalendar(){
        SimpleDateFormat sdf = new SimpleDateFormat("MMM yyyy");
        txtDate.setText(sdf.format(curDate.getTime()));

        ArrayList<DateDay> cells = new ArrayList<>();
        Calendar calendar = (Calendar) curDate.clone();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int prevDays = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        calendar.add(Calendar.DAY_OF_MONTH, -prevDays);

        int maxCellCount = 6*7;
        now = new Date();
        while (cells.size() < maxCellCount)
        {
            Calendar curSelectDate = (Calendar) calendar.clone();
            showDate = (Date) curSelectDate.getTime();
            dateDay = new DateDay();
            dateDay.setDate(showDate);
            if(curDate.getTime().getMonth() == showDate.getMonth())
            {
                dateDay.setMonth(true);
            }
            if(showDate.getDate() == now.getDate() && showDate.getMonth() == now.getMonth() && showDate.getYear() == now.getYear())
            {
                dateDay.setToday(true);
            }
            cells.add(dateDay);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        grid.setAdapter(new CalendarAdapter(CalendarActivity.this, cells));
    }

    private class MyPagerAdapter extends PagerAdapter{

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
//            super.setPrimaryItem(container, position, object);
        }

        @Override
        public int getCount() {
            return 1000;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            GridView gv = initCalendarView(position);
            gv.setId(position);
            container.addView(gv);
            return gv;
        }
    }

    public void initTitle(Calendar cal)
    {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdfn = new SimpleDateFormat("MMM yyyy");
        txtDate.setText(sdfn.format(cal.getTime()));
    }

    public GridView initCalendarView(int position){
        int pos = position - 500;
        GridView view = (GridView) LayoutInflater.from(this).inflate(R.layout.calendar_grid_view, null);

        ArrayList<DateDay> cells = new ArrayList<>();
        Calendar calendar = (Calendar) curDate.clone();
        calendar.add(Calendar.MONTH, pos);
        Calendar isCalendar = (Calendar) calendar.clone();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int prevDays = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        calendar.add(Calendar.DAY_OF_MONTH, -prevDays);

        int maxCellCount = 6*7;
        now = new Date();
        while (cells.size() < maxCellCount)
        {
            Calendar forCalendar = (Calendar) calendar.clone();
            showDate = (Date) forCalendar.getTime();
            dateDay = new DateDay();
            LunarCalendar lunarCalendar = new LunarCalendar();
            String lun = lunarCalendar.getLunarDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), false);
            dateDay.setDayLun(lun);
            dateDay.setDate(showDate);
            if((isCalendar.getTime()).getMonth() == showDate.getMonth())
            {
                dateDay.setMonth(true);
            }
            if(showDate.getDate() == now.getDate() && showDate.getMonth() == now.getMonth() && showDate.getYear() == now.getYear())
            {
                dateDay.setToday(true);
            }
            cells.add(dateDay);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
//        initTitle(isCalendar);
        Log.d("test", "is:"+isCalendar);
        view.setAdapter(new CalendarAdapter(this, cells));
        return view;
    }
    private class CalendarAdapter extends ArrayAdapter<DateDay> {
        LayoutInflater inflater;
        public CalendarAdapter(@NonNull Context context, ArrayList<DateDay> days) {
            super(context, R.layout.calendar_text_day, days);
            inflater = LayoutInflater.from(context);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            DateDay DateDay = getItem(position);
            if (convertView == null){
                convertView = inflater.inflate(R.layout.calendar_text_day, parent, false);
            }
            //断言！为true继续执行，false终止执行、、
            assert DateDay != null;
            ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).isToday = DateDay.isToday();
            ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).isMonth = DateDay.isMonth();
            ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).date = DateDay.getDate();
            ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).lunDate = DateDay.getDayLun();
            ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).initEvent();
//            Date date = DateDay.getDate();
//            TextView dayText = convertView.findViewById(R.id.txt_calendar_day);
//            dayText.setText(String.valueOf(date.getDate()));
//            if(DateDay.isToday())
//            {
//                ((CalendarDayText) convertView.findViewById(R.id.txt_calendar_day)).isToday = true;
//                dayText.setTextColor(Color.parseColor("#ffffff"));
//                dayText.setBackgroundResource(R.drawable.day_text);
//            }
//            if(!DateDay.isMonth())
//            {
//                dayText.setTextColor(Color.parseColor("#c1c1c1"));
//            }

            return convertView;
        }
    }
}
